from rest_framework import serializers
from doctors.models import ProvinceModel, CityModel, DepartmentModel, HospitalModel, DepartmentCategoryModel, \
    DoctorModel, DoctorEvaluationModel, OperationCategoryModel, OperationModel, TreatmentModel, QuestionModel, \
    AnswerModel
from drf_haystack.serializers import HaystackSerializer
from doctors.search_indexes import DoctorIndex


class ProvinceSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProvinceModel
        fields = ['id', 'name']


class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = CityModel
        fields = ['id', 'name', 'is_hot']


class TreatmentSerializer(serializers.ModelSerializer):
    department = serializers.CharField(source='department.name', read_only=True)

    class Meta:
        model = TreatmentModel
        fields = ['id', 'name', 'department', 'remark']


class TreatmentDetailSerializer(serializers.ModelSerializer):
    department = serializers.CharField(source='department.name', read_only=True)
    department_id = serializers.CharField(source='department.id', read_only=True)

    class Meta:
        model = TreatmentModel
        fields = ['id', 'name', 'description', 'department', 'remark', 'department_id']


class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = DepartmentModel
        fields = ['id', 'name']


class DepartmentCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = DepartmentCategoryModel
        fields = ['id', 'name']


class DoctorEvaluationSerializer(serializers.ModelSerializer):
    doctor = serializers.CharField(source='doctors.name', read_only=True)
    user = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = DoctorEvaluationModel
        fields = ['id', 'doctor', 'user', 'content', 'score', 'created_time']


class DoctorSerializer(serializers.ModelSerializer):
    hospital = serializers.CharField(source='hospital.name', read_only=True)
    department = serializers.CharField(source='department.name', read_only=True)
    cities = serializers.CharField(source='city.name', read_only=True)
    department_id = serializers.CharField(source='department.id', read_only=True)

    class Meta:
        model = DoctorModel
        fields = ['id', 'name', 'get_title_display', 'avatar_preview', 'avatar', 'introduction', 'specialty',
                  'department', 'hospital', 'consultation_count', 'consultation_price', 'rating', 'avg_response_time',
                  'city', 'cities', 'department_id']


class DoctorDetailSerializer(serializers.ModelSerializer):
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    hospital_level = serializers.CharField(source='hospital.get_level_display', read_only=True)
    department = serializers.CharField(source='department.name', read_only=True)
    city = serializers.CharField(source='city.name', read_only=True)
    evaluations = DoctorEvaluationSerializer(read_only=True, many=True)

    class Meta:
        model = DoctorModel
        fields = ['id', 'name', 'get_title_display', 'avatar', 'introduction', 'specialty', 'department',
                  'hospital_name', 'hospital_level', 'consultation_count', 'consultation_price', 'rating',
                  'avg_response_time', 'city', 'evaluations']


class HospitalSerializer(serializers.ModelSerializer):
    doctors = DoctorSerializer(read_only=True, many=True)

    class Meta:
        model = HospitalModel
        fields = ['id', 'name', 'address', 'get_level_display', 'covers_small', 'covers', 'doctors']


class HospitalDetailSerializer(serializers.ModelSerializer):
    province = serializers.CharField(source='province.name', read_only=True)
    city = serializers.CharField(source='city.name', read_only=True)
    doctors = DoctorSerializer(read_only=True, many=True)

    class Meta:
        model = HospitalModel
        fields = ['id', 'name', 'address', 'phone', 'get_level_display', 'province', 'city', 'covers_small',
                  'description', 'doctors', 'covers']


class OperationCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = OperationCategoryModel
        fields = ['id', 'name']


class OperationSerializer(serializers.ModelSerializer):
    departments = serializers.CharField(source='department.name', read_only=True)

    class Meta:
        model = OperationModel
        fields = ['id', 'name', 'departments', 'remark']


class OperationDetailSerializer(serializers.ModelSerializer):
    category = serializers.CharField(source='category.name', read_only=True)
    department_name = serializers.CharField(source='departments.name', read_only=True)

    class Meta:
        model = OperationModel
        fields = ['id', 'name', 'category', 'description', 'departments', 'remark', 'department_name']


# class DoctorAnswerSerializer(serializers.ModelSerializer):
#     question_name = serializers.CharField(source='question.name', read_only=True)
#     question_content = serializers.CharField(source='question.content', read_only=True)
#     treatment = serializers.CharField(source='question.treatment.name', read_only=True)
#     doctors = DoctorSerializer(read_only=True)
#     class Meta:
#         model = AnswerModel
#         fields = ['id', 'content','question_name','question_content','created_time','doctors','like_count','treatment']


class AnswerSerializer(serializers.ModelSerializer):
    question_name = serializers.CharField(source='question.name', read_only=True)
    question_content = serializers.CharField(source='question.content', read_only=True)
    question_id = serializers.CharField(source='question.id', read_only=True)
    treatment = serializers.CharField(source='question.treatment.name', read_only=True)
    doctor = DoctorSerializer(read_only=True)

    class Meta:
        model = AnswerModel
        fields = ['id', 'content', 'question_name', 'question_content', 'created_time', 'doctor', 'like_count',
                  'treatment', 'question_id']


class QuestionSerializer(serializers.ModelSerializer):
    department = serializers.CharField(source='department.name', read_only=True)
    treatment = serializers.CharField(source='treatment.name', read_only=True)
    answers = AnswerSerializer(read_only=True, many=True)

    class Meta:
        model = QuestionModel
        fields = ['id', 'name', 'content', 'answers', 'department', 'treatment', 'avatar', 'created_time', 'view_count']


class QuestionDetailSerializer(serializers.ModelSerializer):
    department = serializers.CharField(source='department.name', read_only=True)
    treatment = serializers.CharField(source='treatment.name', read_only=True)
    answers = AnswerSerializer(read_only=True, many=True)

    class Meta:
        model = QuestionModel
        fields = ['id', 'name', 'content', 'answers', 'department', 'treatment', 'avatar', 'created_time', 'view_count']


# 搜索
class DoctorIndexHaystackSerializer(HaystackSerializer):
    class Meta:
        index_classes = [DoctorIndex]
        fields = ["text", "id", "name","avatar", "get_title_display", "introduction", "specialty", "department", "hospital",
                  "hospital_description","hospital_id", "rating", "avg_response_time", "city","consultation_price"]

    def to_representation(self, instance):
        """用于指定返回数据的字段的"""
        instance.avatar = f'http://127.0.0.1:8000/uploads/{instance.avatar}'
        return super().to_representation(instance)

